package com.waitingsnow.modules.giftPlay.view.baseView
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.display.player.BmpDataPlayer;
import com.waitingsnow.core.display.loader.BaseLoader;
import com.waitingsnow.core.model.vo.GiftVO;

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.Dictionary;

/**
 * 
 * @author waitingsnow
 * $Id: SpecialGiftPlayView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class SpecialGiftPlayView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>SpecialGiftPlayView</code> instance.
	 * 
	 */
	public function SpecialGiftPlayView()
	{
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var playRect:Rectangle = new Rectangle(0, 0, 600, 300);
	private var giftVO:GiftVO;
	private var giftMcObj:Object = {};
	private var baseLoader:BaseLoader;
	private var bmpDataPlayArr:Array = [];
	
	//==========================================================================
	//  Properties
	//==========================================================================
	public var runCount:int;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	public function playGift(giftVO:GiftVO):void
	{
		this.giftVO = giftVO;
		var bmpDataArr:Array = giftMcObj[giftVO.giftID];
		if (bmpDataArr)
		{
			startPlay(bmpDataArr);
		}
		else
		{
			baseLoader = new BaseLoader();
			baseLoader.paramObject = giftVO;
			baseLoader.addEventListener(Event.COMPLETE, baseLoaderCompleteHandler);
			baseLoader.startLoad(giftVO.swfPath, null, true);
		}
	}
	
	private function startPlay(bmpDataArr:Array):void
	{
		runCount ++;
		var bmpDataPlayer:BmpDataPlayer = getBmpDataPlayer(bmpDataArr);
		bmpDataPlayer.x = Math.random()*playRect.width + 150;
		bmpDataPlayer.y = Math.random()*playRect.height;
		bmpDataPlayer.addEventListener(Event.COMPLETE, bmpDataPlayerHandler);
		bmpDataPlayer.play();
		addChild(bmpDataPlayer);
	}
	
	private function getBmpDataPlayer(bmpDataArr:Array):BmpDataPlayer
	{
		var result:BmpDataPlayer;
		if (bmpDataPlayArr && bmpDataPlayArr.length)
		{
			result = bmpDataPlayArr.pop();
		}
		else
		{
			result = new BmpDataPlayer();
		}
		result.bmpDataArr = bmpDataArr;
		return result;
	}
	
	private function collectBmpDataPlayer(bmpDataPlayer:BmpDataPlayer):void
	{
		if (bmpDataPlayer && bmpDataPlayer.parent)
		{
			bmpDataPlayer.parent.removeChild(bmpDataPlayer);
		}
		if (bmpDataPlayArr)
		{
			bmpDataPlayArr.push(bmpDataPlayer);
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function baseLoaderCompleteHandler(event:Event):void
	{
		var bmpDataArr:Array = event.target.getBmpDataArr();
		giftMcObj[baseLoader.paramObject.giftID] = bmpDataArr;
		startPlay(bmpDataArr);
	}
	
	private function bmpDataPlayerHandler(event:Event):void
	{
		runCount--;
		collectBmpDataPlayer(BmpDataPlayer(event.target));
		dispatchEvent(new Event(Event.COMPLETE));
	}
}
}